home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13332 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: news.infi.net!usenet
  2. From: Steve Rountree <srndtree@infi.net>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Recursion Question
  5. Date: Sat, 06 Apr 1996 12:06:36 -0800
  6. Organization: InfiNet
  7. Message-ID: <3166CECC.354E@infi.net>
  8. References: <4k14o1$2k2@isis.fiu.edu>
  9. NNTP-Posting-Host: h-agate.dc.infi.net
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (Win16; I)
  14.  
  15. A non-recursive method:  (assuming a 16 bit integer)
  16.  
  17.     for(count=32768;count>0;count/=2){
  18.       printf("%d",(num & count ? 1 : 0));
  19.     }
  20.  
  21.  
  22.   Recursively:
  23.  
  24.    int Binum(int value,int num){
  25.      if(value){
  26.        printf("%d",(value & num ? 1 : 0));
  27.        Binum(value/2,num);
  28.      }
  29.      else
  30.        return(0);
  31.     }
  32.         
  33.      
  34.   I know the first one works and I think that the second one is 
  35. structured properly.  If not, play with it.  I think that it is close.
  36. These, of course, only address the problem with printing out as you go.  
  37. You can also of course store the result of each "&" operation in a 
  38. integer array.
  39.  
  40. Steve Rountree
  41.  
  42. Mark Romano wrote:
  43. > I am trying to find some recursive functions that will convert a number into
  44. > a binary. I need one that will print out the numbers as they are
  45. > generated and I also need one that will hold the binary digits in memory
  46. > and then printed out with a single printf statement.  Can any one help??
  47. > __________________________________________________________
  48. > |                                                        |
  49. > |       Sent to you by: mark@serss1.fiu.edu              |
  50. > |                                                        |
  51. > |________________________________________________________|
  52.